package org.dromara.hm.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.web.core.BaseController;
import org.dromara.hm.domain.bo.HmStockBo;
import org.dromara.hm.domain.vo.HmStockVo;
import org.dromara.hm.domain.vo.StockChangeRecordVO;
import org.dromara.hm.service.IHmStockService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 库存数据
 *
 * @author zy Hu
 * @date 2024-12-15
 */
@Slf4j
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/hm/stock")
public class HmStockController extends BaseController
{

    private final IHmStockService hmStockService;

    /**
     * 查询库存数据列表
     */
    @SaCheckPermission("hm:stock:list")
    @GetMapping("/list")
    public TableDataInfo<HmStockVo> list(HmStockBo bo, PageQuery pageQuery)
    {
        return hmStockService.queryPageList(bo, pageQuery);
    }

    /**
     * 查询库存变更记录
     *
     * @param productId 商品ID
     * @param storeId   店铺ID
     * @param pageNum   页码
     * @param pageSize  每页条数
     *
     * @return 分页结果
     */
    @GetMapping("/change-records")
    public TableDataInfo<StockChangeRecordVO> getStockChangeRecords(@RequestParam Long productId, @RequestParam String storeId, @RequestParam String productType, @RequestParam(defaultValue = "1") int pageNum, @RequestParam(defaultValue = "10") int pageSize)
    {
        log.info("查询库存变更记录, productId:{}, storeId:{}", productId, storeId);
        return hmStockService.getStockChangeRecords(productId, storeId,productType, pageNum, pageSize);
    }

    /**
     * 导出库存数据列表
     */
    @SaCheckPermission("hm:stock:export")
    @Log(title = "库存数据", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HmStockBo bo, HttpServletResponse response)
    {
        List<HmStockVo> list = hmStockService.queryList(bo);
        ExcelUtil.exportExcel(list, "库存数据", HmStockVo.class, response);
    }

    /**
     * 获取库存数据详细信息
     *
     * @param stockId 主键
     */
    @SaCheckPermission("hm:stock:query")
    @GetMapping("/{stockId}")
    public R<HmStockVo> getInfo(@NotNull(message = "主键不能为空") @PathVariable Long stockId)
    {
        return R.ok(hmStockService.queryById(stockId));
    }

    /**
     * 新增库存数据
     */
    @SaCheckPermission("hm:stock:add")
    @Log(title = "库存数据", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody HmStockBo bo)
    {
        return toAjax(hmStockService.insertByBo(bo));
    }

    /**
     * 修改库存数据
     */
    @SaCheckPermission("hm:stock:edit")
    @Log(title = "库存数据", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody HmStockBo bo)
    {
        return toAjax(hmStockService.updateByBo(bo));
    }

    /**
     * 删除库存数据
     *
     * @param stockIds 主键串
     */
    @SaCheckPermission("hm:stock:remove")
    @Log(title = "库存数据", businessType = BusinessType.DELETE)
    @DeleteMapping("/{stockIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空") @PathVariable Long[] stockIds)
    {
        return toAjax(hmStockService.deleteWithValidByIds(List.of(stockIds), true));
    }
}
